<?php


namespace app\api\controller;

use app\api\model\Address as AddressModel;

class Address extends BaseController
{
    /**
     * 获取所有收货地址
     *
     */
    public function getAddressAll()
    {
//        $user_id = session('user_id');
        $user_id = 1;
//        $res = AddressModel::where('user_id', $user_id)->select();
//        return $this->renderSuccess(compact('res'));
        $res = AddressModel::where('user_id',$user_id)->with('region')->select();
        return $this->renderSuccess(compact('res'));
        var_dump($res);
        exit();
    }


    /**
     * 获取默认收货地址
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getAddress()
    {
        $user_id = session('user');
        db('address')->where('user_id', $user_id)->where('default', 1)->find();
    }

    public function addAddress()
    {
        $data = $this->request->param();
        $user_id = session('user');
        db('address')->insert([
            'user_id' => $user_id,
            'username' => $data['username'],
            'province' => $data['province'],
            'city' => $data['city'],
            'area' => $data['area'],
            'address' => $data['address'],
            //是否为默认
            'default' => $data['default'],
            'createtime' => time(),
            'updatetime' => time()
        ]);
    }
}